package iva;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import sueldos.Utility;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.TouchListener;
import org.eclipse.swt.events.TouchEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;


public class CargarFacturaC {
	protected Shell shlIvaListado;
	private Text textProveedor;
	private Composite composite;
	private Composite composite_2;
	private Table table;
	private Text numero;
	private Text importe;
	private Text comprobante;
	private Text tipoFactura;
	private Text fecha;
	private Text sucursal;
	private Button btnGuardar;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	
	
	public void mostrarDatos(String fecha, String empresa){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbeccarvarela", "", ""); 
			String query="select * from facturasbc where (fecha = ? and empresa = ?)";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.setString(1, fecha);
			ps.setString(2, empresa);
			ps.execute();
			
			ResultSet rs = ps.getResultSet();

			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
				
				
					item.setText(0, rs.getString("proveedor"));
					item.setText(1, rs.getString("fecha"));
					item.setText(2, rs.getString("letra"));
					item.setText(3, rs.getString("sucursal"));
					item.setText(4, rs.getString("num_factura"));
					item.setText(5, rs.getString("concepto"));
					item.setText(6, rs.getBigDecimal("total").setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());

			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public void buscarDatos(String base, String tipoBase, String busqueda, String fecha){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+base, "", ""); 
			String query="SELECT distinct nombre, DATE_FORMAT(fecha, '%d-%m-%Y') AS fecha, " +
					"CONCAT(sucursal, '-' ,numero) AS factura, " +
					"letra, neto, iva, total, retencion, certificad, tipocomp FROM "+tipoBase+" " +
							"WHERE (nombre LIKE '%"+busqueda+"%') AND (fecha >= '"+fecha+"') " +
									"ORDER BY nombre, DATE(fecha) DESC";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.execute();
			
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
					if (tipoBase.equals("ventas") && rs.getString(10).equals("8")){
						item.setText(0, rs.getString(1));
						item.setText(1, rs.getString(2));
						item.setText(2, rs.getString(9));
						item.setText(3, "R");
						item.setText(4, "");
						item.setText(5, "");
						item.setText(6, rs.getBigDecimal(8).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
					}else{
						item.setText(0, rs.getString(1));
						item.setText(1, rs.getString(2));
						if (rs.getString(3) == null){
							item.setText(2, "");
						}else {
							item.setText(2, rs.getString(3));
						}
						item.setText(3, rs.getString(4));
						try {
							item.setText(4, rs.getBigDecimal(5).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
							item.setText(5, rs.getBigDecimal(6).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
							item.setText(6, rs.getBigDecimal(7).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
						} catch (Exception e) {
						}
					}
			}
			ps.close();
			conexion.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void buscarFactura(String base, String tipoBase, String busqueda, String sucursa, String number){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+base, "", ""); 
			
			//String query="SELECT distinct nombre, DATE_FORMAT(fecha, '%d-%m-%Y') AS fecha, CONCAT(sucursal, '-' ,numero) AS factura, letra, neto, iva, total, retencion, certificad, tipocomp FROM "+tipoBase+" WHERE (nombre LIKE '%"+busqueda+"%') AND (SUCURSAL = "+sucursa+" AND numero = "+numero+") ORDER BY nombre asc";
			
			String query="SELECT distinct nombre, DATE_FORMAT(fecha, '%d-%m-%Y') AS fecha, CONCAT(sucursal, '-' ,numero) AS factura, letra, neto, iva, total, retencion, certificad, tipocomp FROM "+tipoBase+" WHERE ((nombre LIKE ?) AND (SUCURSAL = ? AND numero = ?)) ORDER BY YEAR(fecha) desc, nombre asc";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.setString(1, "%"+busqueda+"%");
			ps.setString(2, sucursa);
			ps.setString(3, number);
			ps.execute();
			
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
					if (tipoBase.equals("ventas") && rs.getString(10).equals("8")){
						item.setText(0, rs.getString(1));
						item.setText(1, rs.getString(2));
						item.setText(2, rs.getString(9));
						item.setText(3, "R");
						item.setText(4, "");
						item.setText(5, "");
						item.setText(6, rs.getBigDecimal(8).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
					}else{
						item.setText(0, rs.getString(1));
						item.setText(1, rs.getString(2));
						if (rs.getString(3) == null){
							item.setText(2, "");
						}else {
							item.setText(2, rs.getString(3));
						}
						item.setText(3, rs.getString(4));
						try {
							item.setText(4, rs.getBigDecimal(5).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
							item.setText(5, rs.getBigDecimal(6).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
							item.setText(6, rs.getBigDecimal(7).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
						} catch (Exception e) {
							
						}
					}
			}
			ps.close();
			conexion.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					CargarFacturaC window = new CargarFacturaC();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlIvaListado.open();
		shlIvaListado.layout();
		while (!shlIvaListado.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	
	
	
	
	
	protected void createContents() {
		shlIvaListado = new Shell(SWT.MAX | SWT.MIN | SWT.CLOSE);
		shlIvaListado.setSize(857, 476);
		//shell.setSize(754, 538);
		shlIvaListado.setImage(SWTResourceManager.getImage(CargarFacturaC.class, "/imagenes/AppIcon.png"));
		shlIvaListado.setText("IVA - Cargar Factura B y C");
		shlIvaListado.setLayout(new FormLayout());
		

		Rectangle bds = Display.getCurrent().getClientArea();
        Point p = shlIvaListado.getSize();

        int nLeft = (bds.width - p.x) / 2;
        int nTop = (bds.height - p.y) / 2;

        shlIvaListado.setBounds(nLeft, nTop, p.x, p.y);
		
		
		
		composite = new Composite(shlIvaListado, SWT.BORDER);
		composite.setLayout(new FormLayout());
		FormData fd_composite = new FormData();
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.right = new FormAttachment(100, -10);
		composite.setLayoutData(fd_composite);
		
		composite_2 = new Composite(shlIvaListado, SWT.BORDER);
		fd_composite.bottom = new FormAttachment(composite_2, -6);
		composite_2.setFont(SWTResourceManager.getFont("Calibri", 8, SWT.NORMAL));
		composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_composite_2 = new FormData();
		fd_composite_2.bottom = new FormAttachment(100, -10);
		fd_composite_2.top = new FormAttachment(0, 166);
		fd_composite_2.left = new FormAttachment(0, 10);
		fd_composite_2.right = new FormAttachment(100, -10);
		composite_2.setLayoutData(fd_composite_2);
		
		table = new Table(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
		table.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.NORMAL));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnProveedor = new TableColumn(table, SWT.NONE);
		tblclmnProveedor.setWidth(155);
		tblclmnProveedor.setText("Proveedor");
		
		TableColumn tblclmnFecha = new TableColumn(table, SWT.NONE);
		tblclmnFecha.setWidth(80);
		tblclmnFecha.setText("Fecha");
		
		TableColumn tblclmnTipo = new TableColumn(table, SWT.NONE);
		tblclmnTipo.setWidth(44);
		tblclmnTipo.setText("Letra");
		
		TableColumn tblclmnSubtotal = new TableColumn(table, SWT.RIGHT);
		tblclmnSubtotal.setWidth(85);
		tblclmnSubtotal.setText("Sucursal");
		
		TableColumn tblclmnIva = new TableColumn(table, SWT.RIGHT);
		tblclmnIva.setWidth(117);
		tblclmnIva.setText("Numero");
		
		TableColumn tblclmnFactura = new TableColumn(table, SWT.NONE);
		tblclmnFactura.setWidth(82);
		tblclmnFactura.setText("Concepto");
		
		TableColumn tblclmnTotal = new TableColumn(table, SWT.RIGHT);
		tblclmnTotal.setWidth(101);
		tblclmnTotal.setText("Total");
		
		Label lblProveedor = new Label(composite, SWT.NONE);
		FormData fd_lblProveedor = new FormData();
		fd_lblProveedor.top = new FormAttachment(0, 10);
		fd_lblProveedor.left = new FormAttachment(0, 5);
		lblProveedor.setLayoutData(fd_lblProveedor);
		lblProveedor.setText("Proveedor:");
		lblProveedor.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		
		textProveedor = new Text(composite, SWT.BORDER);
		FormData fd_textProveedor = new FormData();
		fd_textProveedor.bottom = new FormAttachment(0, 33);
		fd_textProveedor.right = new FormAttachment(0, 193);
		fd_textProveedor.top = new FormAttachment(0, 5);
		fd_textProveedor.left = new FormAttachment(0, 79);
		textProveedor.setLayoutData(fd_textProveedor);
		textProveedor.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				textProveedor.setText("");
			}
		});
		textProveedor.setForeground(SWTResourceManager.getColor(0, 0, 0));
		textProveedor.setText("Ingrese el nombre completo o parte.");
		textProveedor.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		Label lblEmpresa = new Label(composite, SWT.NONE);
		FormData fd_lblEmpresa = new FormData();
		fd_lblEmpresa.bottom = new FormAttachment(0, 60);
		fd_lblEmpresa.top = new FormAttachment(0, 41);
		fd_lblEmpresa.left = new FormAttachment(0, 17);
		lblEmpresa.setLayoutData(fd_lblEmpresa);
		lblEmpresa.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		lblEmpresa.setText("Empresa:");
		
		final Combo comboEmpresa = new Combo(composite, SWT.READ_ONLY);
		FormData fd_comboEmpresa = new FormData();
		fd_comboEmpresa.right = new FormAttachment(0, 447);
		fd_comboEmpresa.top = new FormAttachment(0, 38);
		fd_comboEmpresa.left = new FormAttachment(0, 79);
		comboEmpresa.setLayoutData(fd_comboEmpresa);
		comboEmpresa.setVisibleItemCount(12);
		comboEmpresa.setItems(new String[] {"SELECCIONAR EMPRESA.......", "AZTIRIA ELSA CLARA", "CLAVIER", 
				"ESTANCIA EL ALBARDON S.A.", "ESTANCIA PAMPA GRANDE S.A.", "ISABEL DUGGAN DE DI CAMPELLO", 
				"KENVIS S.A.", "LOS HUAICOS M.L. S.A.", "TRES ISLAS S.A."});
		
		comboEmpresa.setData("AZTIRIA ELSA CLARA","24"); 
		comboEmpresa.setData("CLAVIER","2"); 
		comboEmpresa.setData("ESTANCIA EL ALBARDON S.A.","5"); 
		comboEmpresa.setData("ESTANCIA PAMPA GRANDE S.A.","4"); 
		comboEmpresa.setData("ISABEL DUGGAN DE DI CAMPELLO","6"); 
		comboEmpresa.setData("KENVIS S.A.","9"); 
		comboEmpresa.setData("LOS HUAICOS M.L. S.A.","13"); 
		comboEmpresa.setData("TRES ISLAS S.A.","11"); 
		
		comboEmpresa.select(0);

		comboEmpresa.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		Label lblPeriodo = new Label(composite, SWT.NONE);
		FormData fd_lblPeriodo = new FormData();
		fd_lblPeriodo.left = new FormAttachment(0, 25);
		fd_lblPeriodo.top = new FormAttachment(lblEmpresa, 13);
		lblPeriodo.setLayoutData(fd_lblPeriodo);
		lblPeriodo.setText("Factura:");
		lblPeriodo.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		sucursal = new Text(composite, SWT.BORDER);
		sucursal.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
					numero.setFocus();
				}
			}
		});
		fd_lblPeriodo.bottom = new FormAttachment(sucursal, 0, SWT.BOTTOM);
		FormData fd_sucursal = new FormData();
		fd_sucursal.top = new FormAttachment(comboEmpresa, 6);
		sucursal.setLayoutData(fd_sucursal);
		sucursal.setToolTipText("");
		sucursal.setForeground(SWTResourceManager.getColor(0, 0, 0));
		
		sucursal.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 14, SWT.BOLD));
		
		sucursal.setTextLimit(5);
		Label label = new Label(composite, SWT.NONE);
		FormData fd_label = new FormData();
		fd_label.top = new FormAttachment(0, 78);
		fd_label.left = new FormAttachment(0, 515);
		label.setLayoutData(fd_label);
		
		numero = new Text(composite, SWT.BORDER);
		numero.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
					importe.setFocus();
				}
			}
		});
		fd_sucursal.right = new FormAttachment(numero, -6);
		FormData fd_numero = new FormData();
		fd_numero.left = new FormAttachment(0, 223);
		fd_numero.top = new FormAttachment(comboEmpresa, 6);
		fd_numero.right = new FormAttachment(label, -87);
		fd_numero.bottom = new FormAttachment(100, -48);
		numero.setLayoutData(fd_numero);
		numero.setToolTipText("");
		numero.setTextLimit(10);
		numero.setForeground(SWTResourceManager.getColor(0, 0, 0));
		numero.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 14, SWT.BOLD));
		
		Button btnBuscarProveedor = new Button(composite, SWT.NONE);
		FormData fd_btnBuscarProveedor = new FormData();
		fd_btnBuscarProveedor.top = new FormAttachment(lblProveedor, -5, SWT.TOP);
		fd_btnBuscarProveedor.left = new FormAttachment(textProveedor, 6);
		btnBuscarProveedor.setLayoutData(fd_btnBuscarProveedor);
		btnBuscarProveedor.setText("Buscar Proveedor");
		
		Label lblImporte = new Label(composite, SWT.NONE);
		lblImporte.setText("Importe:");
		lblImporte.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		FormData fd_lblImporte = new FormData();
		fd_lblImporte.top = new FormAttachment(0, 109);
		fd_lblImporte.right = new FormAttachment(lblProveedor, 0, SWT.RIGHT);
		lblImporte.setLayoutData(fd_lblImporte);
		
		importe = new Text(composite, SWT.BORDER);
		importe.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
				btnGuardar.setFocus();
				}
			}
		});
		fd_sucursal.bottom = new FormAttachment(100, -48);
		importe.setToolTipText("");
		importe.setTextLimit(10);
		importe.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(0, 0, 0));
		importe.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 14, SWT.BOLD));
		FormData fd_importe = new FormData();
		fd_importe.left = new FormAttachment(lblImporte, 5);
		fd_importe.top = new FormAttachment(sucursal, 6);
		importe.setLayoutData(fd_importe);
		
		comprobante = new Text(composite, SWT.BORDER | SWT.CENTER);
		comprobante.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(SWT.COLOR_RED));
		comprobante.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Segoe UI", 22, SWT.BOLD));
		comprobante.setTextLimit(4);
		FormData fd_comprobante = new FormData();
		fd_comprobante.top = new FormAttachment(0);
		fd_comprobante.right = new FormAttachment(100);
		fd_comprobante.left = new FormAttachment(100, -132);
		fd_comprobante.bottom = new FormAttachment(0, 47);
		comprobante.setLayoutData(fd_comprobante);
		
		tipoFactura = new Text(composite, SWT.BORDER | SWT.CENTER);
		tipoFactura.setTouchEnabled(true);
		tipoFactura.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){
					sucursal.setFocus();
				}
			}
		});
		fd_sucursal.left = new FormAttachment(0, 126);
		fd_lblPeriodo.right = new FormAttachment(tipoFactura, -5);
		tipoFactura.setToolTipText("");
		tipoFactura.setTextLimit(1);
		tipoFactura.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(0, 0, 0));
		tipoFactura.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 14, SWT.BOLD));
		FormData fd_tipoFactura = new FormData();
		fd_tipoFactura.top = new FormAttachment(comboEmpresa, 6);
		fd_tipoFactura.bottom = new FormAttachment(importe, -6);
		fd_tipoFactura.right = new FormAttachment(sucursal, -6);
		fd_tipoFactura.left = new FormAttachment(0, 79);
		tipoFactura.setLayoutData(fd_tipoFactura);
		
		Label lblFecha = new Label(composite, SWT.NONE);
		lblFecha.setText("Fecha:");
		lblFecha.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		FormData fd_lblFecha = new FormData();
		fd_lblFecha.top = new FormAttachment(lblEmpresa, 0, SWT.TOP);
		fd_lblFecha.left = new FormAttachment(comboEmpresa, 6);
		lblFecha.setLayoutData(fd_lblFecha);
		
		fecha = new Text(composite, SWT.BORDER);
		fd_btnBuscarProveedor.bottom = new FormAttachment(fecha, -7);
		fecha.setToolTipText("Formato: 01/01/2000");
		fecha.setTextLimit(12);
		fecha.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(0, 0, 0));
		fecha.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		FormData fd_fecha = new FormData();
		fd_fecha.top = new FormAttachment(comboEmpresa, -24);
		fd_fecha.bottom = new FormAttachment(comboEmpresa, 0, SWT.BOTTOM);
		fd_fecha.right = new FormAttachment(lblFecha, 110, SWT.RIGHT);
		fd_fecha.left = new FormAttachment(lblFecha, 6);
		fecha.setLayoutData(fd_fecha);
		
		final Button mantenerDatos = new Button(composite, SWT.CHECK);
		FormData fd_mantenerDatos = new FormData();
		fd_mantenerDatos.top = new FormAttachment(numero, 14);
		fd_mantenerDatos.right = new FormAttachment(100, -368);
		mantenerDatos.setLayoutData(fd_mantenerDatos);
		mantenerDatos.setText("Mantener Datos");
		
		btnGuardar = new Button(composite, SWT.NONE);
		fd_importe.bottom = new FormAttachment(btnGuardar, 0, SWT.BOTTOM);
		fd_mantenerDatos.left = new FormAttachment(0, 360);
		fd_importe.right = new FormAttachment(btnGuardar, -6);
		FormData fd_btnGuardar = new FormData();
		fd_btnGuardar.right = new FormAttachment(mantenerDatos, -6);
		fd_btnGuardar.left = new FormAttachment(0, 210);
		fd_btnGuardar.bottom = new FormAttachment(lblImporte, 9, SWT.BOTTOM);
		fd_btnGuardar.top = new FormAttachment(sucursal, 6);
		btnGuardar.setLayoutData(fd_btnGuardar);
		btnGuardar.setText("Guardar");
		
		final Label nombreProveedor = new Label(composite, SWT.NONE);
		nombreProveedor.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(SWT.COLOR_RED));
		nombreProveedor.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("Calibri", 14, SWT.BOLD));
		FormData fd_nombreProveedor = new FormData();
		fd_nombreProveedor.right = new FormAttachment(fecha, 74, SWT.RIGHT);
		fd_nombreProveedor.bottom = new FormAttachment(textProveedor, 0, SWT.BOTTOM);
		fd_nombreProveedor.top = new FormAttachment(0, 5);
		fd_nombreProveedor.left = new FormAttachment(btnBuscarProveedor, 6);
		nombreProveedor.setLayoutData(fd_nombreProveedor);
		composite.setTabList(new Control[]{textProveedor, btnBuscarProveedor, comprobante, comboEmpresa, fecha, tipoFactura, sucursal, numero, importe, btnGuardar});
		
		Menu menu = new Menu(shlIvaListado, SWT.BAR);
		shlIvaListado.setMenuBar(menu);
		
		MenuItem mntmArchivo = new MenuItem(menu, SWT.CASCADE);
		mntmArchivo.setText("Archivo");
		
		Menu menu_1 = new Menu(mntmArchivo);
		mntmArchivo.setMenu(menu_1);
		
		MenuItem mntmSalir = new MenuItem(menu_1, SWT.NONE);
		mntmSalir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlIvaListado.close();
			}
		});
		mntmSalir.setText("Salir");
		
		MenuItem mntmProveedores = new MenuItem(menu, SWT.CASCADE);
		mntmProveedores.setText("Proveedores");
		
		Menu menu_2 = new Menu(mntmProveedores);
		mntmProveedores.setMenu(menu_2);
		
		MenuItem mntmBuscarProveedor = new MenuItem(menu_2, SWT.NONE);
		mntmBuscarProveedor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new BuscarProveedor().open();
			}
		});
		mntmBuscarProveedor.setText("Buscar Proveedor");
		
		MenuItem mntmFactura = new MenuItem(menu, SWT.CASCADE);
		mntmFactura.setText("Factura");
		
		Menu menu_3 = new Menu(mntmFactura);
		mntmFactura.setMenu(menu_3);
		
		MenuItem mntmBuscarFactura = new MenuItem(menu_3, SWT.NONE);
		mntmBuscarFactura.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.removeAll();
				BuscarFactura fc = new BuscarFactura();
				fc.open();
				System.out.println(fc.getEmpresa());
				System.out.println(fc.getTipobase());
				System.out.println(fc.getNombreproveedor());
				System.out.println(fc.getSucursal());
				System.out.println(fc.getFactura());
				
				
				
				
				
				buscarFactura(fc.getEmpresa(), fc.getTipobase(), fc.getNombreproveedor(), fc.getSucursal(), fc.getFactura());
			}
		});
		mntmBuscarFactura.setText("Buscar Factura");

		btnBuscarProveedor.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					BuscarProveedor bp = new BuscarProveedor();
					bp.open();
					textProveedor.setText(bp.getCodigoProveedor());
					nombreProveedor.setText(bp.getNombreProveedor());
					
				} catch (Exception e2) {
					
				}
			}
		});


		//TODO
		textProveedor.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent ev) {
				try {
					if (ev.keyCode == SWT.CR || ev.keyCode == SWT.KEYPAD_CR){
						if (textProveedor.getText() != ""){
							nombreProveedor.setText(new Proveedor().getProveedorName(textProveedor.getText()));
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
					
				
			}
		});

		
		btnGuardar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					int contador = Integer.valueOf(comprobante.getText());
					
					
					FacturaC facC = new FacturaC(Integer.valueOf(comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex())).toString()), fecha.getText(), 
							textProveedor.getText().toString(), sucursal.getText(), numero.getText(), 
							tipoFactura.getText().toUpperCase(), Integer.valueOf(comprobante.getText().toString()), 
							new BigDecimal(importe.getText().toString()));
					
					/*
					System.out.println(comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex())));
					textProveedor.getText();
					comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex()));
					fecha.getText();
					tipoFactura.getText();
					sucursal.getText();
					numero.getText();
					importe.getText();
					comprobante.getText();*/
					
					facC.insertarFactura();

					if (mantenerDatos.getSelection()){
						numero.setText("");
						importe.setText("");
						numero.setFocus();
					}else{
						tipoFactura.setText("");
						sucursal.setText("");
						numero.setText("");
						importe.setText("");
						textProveedor.setText("");
						nombreProveedor.setText("");
						textProveedor.setFocus();
					}
					contador++;
					comprobante.setText(""+contador);
					table.removeAll();
					mostrarDatos(fecha.getText(), comboEmpresa.getData(comboEmpresa.getItem(comboEmpresa.getSelectionIndex())).toString());
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		});

		
	}
}
